﻿/*
namespace FitnessTracker.Observers
{
   using System;
    using System.IO;
    using System.Threading;
    using System.Timers;
    using FitnessTracker.Observables;
    using FitnessTracker.Packet;
    using FitnessTrackerWPF.Exceptions;
    using Timer = System.Timers.Timer;

    public class ConnectionMonitor : IObserver<HxMPacket>
    {
        private const byte AlarmThresholdInSeconds = 5;
        private const byte NumberOfReconnectAttempts = 10;
        private const byte SecondsBetweenReconnectAttempts = 20;

        private static DateTime lastPacketReceived;
        private static bool connectionEstablished;
        private static TimeSpan alarmThreshold;
        private static HxMReader monitoredHxM;
        private readonly IDisposable cancellation;
        
        public ConnectionMonitor(HxMReader hrSensor)
        {
            cancellation = hrSensor.Subscribe(this);
            monitoredHxM = hrSensor;
            alarmThreshold = new TimeSpan(0, 0, AlarmThresholdInSeconds);
            lastPacketReceived = DateTime.Now;
            var connectionMonitoringTimer = new Timer { Interval = AlarmThresholdInSeconds * 1000 };
            connectionMonitoringTimer.Elapsed += ConnectionMonitorTimerEvent;
            connectionMonitoringTimer.Start();
        }

        public bool IsHxMConnectionAlive()
        {
            return (DateTime.Now.Subtract(lastPacketReceived) < alarmThreshold) && connectionEstablished;
        }

        public void OnNext(HxMPacket packet)
        {
            lastPacketReceived = DateTime.Now;
            connectionEstablished = true;
        }

        public void OnError(Exception error)
        {
            throw new NotImplementedException();
        }

        public void OnCompleted()
        {
            throw new NotImplementedException();
        }

        public void Unsubscribe()
        {
            cancellation.Dispose();
        }

        private static void ConnectionMonitorTimerEvent(object source, ElapsedEventArgs e)
        {
            if (DateTime.Now.Subtract(lastPacketReceived) > alarmThreshold)
            {
                AttemptToReconnect();
            }
        }

        private static void AttemptToReconnect()
        {
           connectionEstablished = false;
            monitoredHxM.ClosePort();
            for (var reconnectAttempt = 0; reconnectAttempt < NumberOfReconnectAttempts; reconnectAttempt++)
            {
                Thread.Sleep(SecondsBetweenReconnectAttempts * 1000);
                Console.WriteLine("Attempting to reconnect {0}, reconnect attempt #{1}", DateTime.Now.ToLongTimeString(), reconnectAttempt);

                if (monitoredHxM.IsConnectedToComPort)
                {
                    break;
                }

                try
                {
                   monitoredHxM.OpenPort();
                }
                catch (UnauthorizedAccessException)
                {
                }
                catch (IOException)
                {
                }
            }

            throw new MissedPacketsThresholdReachedException("Unable to reconnect to HxMReader");
        }
    }
}
    */